package com.cy.pj.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.vo.PageObject;
import com.cy.pj.sys.dao.BasePageDao;
import com.cy.pj.sys.entity.SysRole;
import com.cy.pj.sys.service.BasePageService;

public abstract class BasePageServiceImpl<T> implements BasePageService<T> {
	
	private BasePageDao<T> basePageDao;
	@Autowired
	public BasePageServiceImpl(BasePageDao<T> basePageDao) {
		this.basePageDao=basePageDao;
	}
	
	@Override
	public PageObject<T> findPageObjects(String name, Integer pageCurrent) {
		if (pageCurrent==null||pageCurrent<1) {
			throw new IllegalArgumentException("当前页码不正确");
		}
		int rowCount=basePageDao.getRowCount(name);
		if (rowCount==0) {
			throw new ServiceException("没有找到记录");
		}
		int pageSize=3;
		int startIndex=(pageCurrent-1)*pageSize;
		List<T> records=
				basePageDao.findPageObjects(name, startIndex, pageSize);
		PageObject<T> pageObject=new PageObject<>();
		pageObject.setPageCurrent(pageCurrent);
		pageObject.setPageSize(pageSize);
		pageObject.setRowCount(rowCount);
		pageObject.setRecords(records);
		pageObject.setPageCount((rowCount-1)/pageSize+1);
		return pageObject;
	}
	
}
